﻿using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WEX.Logging.Interop;
using Microsoft.Win32;

namespace Microsoft.Reputation.Tests.FrontEndCommon
{
    /// <summary>
    /// Holds perf counters for Delivery Service
    /// </summary>
    [Serializable]
    public class MrsPerfCounter
    {
        private const string categoryName = "Reputation Services";
        private string machineName = string.Empty;
        private string Version = string.Empty;
        private string TotalLookupCacheHitsCounterName = string.Empty;
        private string TotalLookupCacheMissesCounterName = string.Empty;
        private const string regKeyToGet = @"SOFTWARE\Microsoft\MSAS\DeliveryService\V1.1";
        private const string versionRegKeyName = "Build";

        private static PerformanceCounter lookupCacheHits = null;
        private static PerformanceCounter lookupCacheMisses = null;

        /// <summary>
        /// Constructs MrsPerfCounter object with the web server machine name hosting the Delivery Service
        /// Also reads the version of the delivery service binaries on the machine from registry key for initialization
        /// </summary>
        /// <param name="name">Name of the Web server machine</param>
        public MrsPerfCounter(string name)
        {
            machineName = name;
            RegistryKey regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, name);
            RegistryKey dsRegKey = regKey.OpenSubKey(regKeyToGet);
            Version dsVersion = new Version(dsRegKey.GetValue(versionRegKeyName).ToString());
            this.Version = "V" + dsVersion.Major.ToString();
            TotalLookupCacheHitsCounterName = "DeliveryService." + Version + ".LookupCacheHitsTotal";
            TotalLookupCacheMissesCounterName = "DeliveryService." + Version + ".LookupCacheMissesTotal";
        }

        /// <summary>
        /// Reads the LookupCacheHitsTotal perf counter
        /// </summary>
        /// <returns>Current value of the total cache hits perf counter</returns>
        public float ReadCacheHitValue()
        {
            float cacheHitValue = 0;
            if (!PerformanceCounterCategory.Exists(categoryName, machineName))
            {
                throw new Exception("Error! Performance counter category - Reputation Services - does not exist");
            }

            lookupCacheHits = new PerformanceCounter(categoryName, TotalLookupCacheHitsCounterName, "", machineName);
            cacheHitValue = lookupCacheHits.NextValue();
            Log.Comment("Cache Hit Counter Value = " + cacheHitValue.ToString());
            return cacheHitValue;
        }

        /// <summary>
        /// Reads the LookupCacheMissesTotal perf counter
        /// </summary>
        /// <returns>Current value of the total cache misses perf counter</returns>
        public float ReadCacheMissValue()
        {
            float cacheMissValue = 0;
            if (!PerformanceCounterCategory.Exists(categoryName, machineName))
            {
                throw new Exception("Error! Performance counter category - Reputation Services - does not exist");
            }

            lookupCacheMisses = new PerformanceCounter(categoryName, TotalLookupCacheMissesCounterName, "", machineName);
            cacheMissValue = lookupCacheMisses.NextValue();
            Log.Comment("Cache Miss Counter Value = " + cacheMissValue.ToString());
            return cacheMissValue;
        }
    }
}
